//
//  ZZAboutController.m
//  QQplayground
//
//  Created by zhangxy on 2018/8/30.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZAboutController.h"



#import "ZZDetailHeaderCell.h"

#import "ZZAboutCell.h"
#define cellIdentifier @"ZZAboutCell"


@interface ZZAboutController (){
    NSMutableArray *mulArray;
    int index;
}

@property(nonatomic,strong)  NSMutableArray *listArray;
@property(nonatomic,strong)  UITableView *listTable;

@end

@implementation ZZAboutController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"关于我们";
    [self setNavigationBar];
    
    self.navigationController.navigationBarHidden = NO;
    
    // Do any additional setup after loading the view.
    [self createTableView];
    [_listTable reloadData];
    
    mulArray = [[NSMutableArray alloc] init];
    index = -1;
    [self getAddresss];
}


-(void) getAddresss{
    [NetworkUtil POST:@"http://61.147.113.78/aos2/tree/regionlist" parameters:@{} success:^(id responseObject, NSDictionary *dict) {
        NSArray *arr = dict[@"data"];
        for (NSDictionary *item in arr) {
            NSMutableDictionary *mulDict = [[NSMutableDictionary alloc] initWithDictionary:item];
            
            [mulArray addObject:mulDict];
            
            index = 0;
        }
    } failure:^(NSError *error, NSString *message) {
        
    }];
}

-(void)insertCity{
    if(mulArray.count == index){
        NSLog(@"%@",mulArray);
        NSDictionary *params3 = [NSDictionary dictionaryWithObjectsAndKeys:
                                 mulArray, @"json",
                                 nil];
        //convert object to data
        NSData* jsonData =[NSJSONSerialization dataWithJSONObject:params3
                                                          options:NSJSONWritingPrettyPrinted error:nil];
        //print out the data contents
        NSString* text =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        return;
    }
    
    NSMutableDictionary *item = mulArray[index];
    [NetworkUtil POST:@"http://61.147.113.78/aos2/tree/citylist" parameters:@{@"p_id":item[@"p_id"]} success:^(id responseObject, NSDictionary *dict1) {
            item[@"citylist"] = dict1[@"data"];
        
        index = index + 1;
        if(mulArray.count == index){
            NSLog(@"%@",mulArray);
            NSDictionary *params3 = [NSDictionary dictionaryWithObjectsAndKeys:
                                     mulArray, @"json",
                                     nil];
            //convert object to data
            NSData* jsonData =[NSJSONSerialization dataWithJSONObject:params3
                                                              options:NSJSONWritingPrettyPrinted error:nil];
            //print out the data contents
            NSString* text =[[NSString alloc] initWithData:jsonData
                                                  encoding:NSUTF8StringEncoding];
        }
    } failure:^(NSError *error, NSString *message) {
        index = index + 1;
    }];
}


- (UIView *)headerView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 180)];
    [headerView setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    
    UIImageView *img = [self createImageView];
    [img setFrame:CGRectMake(ScreenWidth / 2 - 90/2, 20, 90, 90)];
    img.layer.cornerRadius = 10.0f;
    img.layer.masksToBounds = YES;
    [img setImage:[UIImage imageNamed:@"icon_app"]];
    [headerView addSubview:img];
    
    
    UILabel *lab = [self createLabel];
    [lab setFrame:CGRectMake(20, 120, ScreenWidth - 40, 30)];
    [lab setTextColor:UIColor.whiteColor];
    [lab setTextAlignment:NSTextAlignmentCenter];
    [lab setFont:FontSeventeen];
    [lab setText:[NSString stringWithFormat:@"version %@",zcGetAppVersion()]];
    [headerView addSubview:lab];
    
    return headerView;
}


-(void)createTableView{
    _listArray = [[NSMutableArray alloc] init];
    
    _listTable=[self.view createTableView:self cell:cellIdentifier];
    [_listTable setFrame:self.view.bounds];
    
    [_listTable setBackgroundColor:[UIColor clearColor]];
    _listTable.bounces = NO;
    if (iOS7) {
        _listTable.backgroundView = nil;
    }
    
    _listTable.tableHeaderView = [self headerView];
    
    [_listTable setSeparatorColor:LINE_COLOR];
    [_listTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    
    
    [self setTableSeparatorInset];
}

#pragma mark UITableView delegate Start
// 返回section数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

// 返回section高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

// 返回section 的View
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    
    
    return headerView;
}

// 返回section下得行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZZAboutCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ZZAboutCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
    }
    
    [cell dataToView:indexPath.row];
    return cell;
}



// 是否显示删除功能
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

// 删除清理数据
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingStyle = UITableViewCellEditingStyleDelete;
}


// table 行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    return 70.0f;
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

// table 行的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *text = @"";
    if(indexPath.row == 0){
        text = @"933840@qq.com";
    }else{
        text = @"Q友乐园";
    }
    [self insertCity];
    
//    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
//    pasteboard.string = convertToString(text);
//
//    [PopUtil showMessage:@"已复制到剪切板"];
    
    
}

//设置分割线间距
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if((indexPath.row+1) < _listArray.count){
        UIEdgeInsets inset = UIEdgeInsetsMake(0, 10, 0, 0);
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:inset];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:inset];
        }
    }
}

-(void)viewDidLayoutSubviews{
    [self setTableSeparatorInset];
}
#pragma mark UITableView delegate end

/**
 *  设置UITableView分割线空隙
 */
-(void)setTableSeparatorInset{
    UIEdgeInsets inset = UIEdgeInsetsMake(0, 10, 0, 0);
    if ([_listTable respondsToSelector:@selector(setSeparatorInset:)]) {
        [_listTable setSeparatorInset:inset];
    }
    
    if ([_listTable respondsToSelector:@selector(setLayoutMargins:)]) {
        [_listTable setLayoutMargins:inset];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
